package com.codeying.servlet.rentorder;

import com.codeying.servlet.AbsServlet;
import com.codeying.entity.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;
import com.codeying.utils.*;
import com.codeying.utils.component.*;

/**
 * 保存和新增租车订单
 */
@WebServlet("/rentorder/save")
public class SaveRentorderServlet extends AbsServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        resp.setCharacterEncoding("UTF-8");
        int res=0;
        Rentorder entityTemp=new Rentorder ();
        //将参数全部转化成Map格式
        Map<String, Object> reqMap = CommonUtils.requestToMap(req);
        String id = (String) reqMap.get("id");
        entityTemp.setId(Integer.parseInt(id));
                                        String userid = ""; //用户
                                            String cid = ""; //汽车
                                            Date createtime = null; //订单时间
                                            Double price = 0.0;//单价
                                            Integer count = 0;//租车天数
                                            Double tprice = 0.0;//总价格
                                            String status = ""; //租车状态
                                        userid = (String)reqMap.get("userid");
            entityTemp.setUserid (Integer.parseInt(userid));//String类型参数
                cid = (String)reqMap.get("cid");
            entityTemp.setCid (Integer.parseInt(cid));//String类型参数
                createtime = DateUtil.strToDate((String)reqMap.get("createtime"));
            entityTemp.setCreatetime (createtime);
                    if (reqMap.get("price") != null && !reqMap.get("price").equals("")){
                price = Double.valueOf((String) reqMap.get("price"));
                entityTemp.setPrice (price);
            }
                    if (reqMap.get("count") != null && ! reqMap.get("count").equals("")) {
                count = Integer.valueOf((String) reqMap.get("count"));
                entityTemp.setCount (count);
            }
                    if (reqMap.get("tprice") != null && !reqMap.get("tprice").equals("")){
                tprice = Double.valueOf((String) reqMap.get("tprice"));
                entityTemp.setTprice (tprice);
            }
                    status = (String)reqMap.get("status");
            entityTemp.setStatus (status);//String类型参数

        //新增或更新
        if(entityTemp.getId() == 0 || "".equals(entityTemp.getId())){//新增
        id = String.valueOf(CommonUtils.newId());
        entityTemp.setId (Integer.parseInt(id));
        createtime = new Date();
        entityTemp.setCreatetime (createtime);
        price = carService.getById(cid).getPrice();
        entityTemp.setPrice (price);
        tprice = count*price;
        entityTemp.setTprice (tprice);
        status = "已下单";
        entityTemp.setStatus (status);
        //如果有唯一字段，则在此校验 （字段不为空在前端校验）
                                                                                                                //=========添加之前

            //=========添加之前
            res = rentorderService.insert(entityTemp);
        }else {
            //=========修改之前

            //=========修改之前
            res = rentorderService.updateById(entityTemp);
        }

        if(res !=0){
            resp.getWriter().print(ApiResult.success());//标记保存成功
        }else {
            resp.getWriter().print(ApiResult.fail("保存失败"));//保存失败
        }
    }
}

